package com.yaodian100.mall.member.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.utils.StringUtils;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.core.menu.service.MenuService;
import com.yaodian100.ec.generate.service.GenerateOrderService;
import com.yaodian100.mall.member.util.UserData;

public class MyOrderAction extends ActionSupport implements Preparable {
	@Autowired
	GenerateOrderService gos;
	@Autowired
	MenuService menuService;

	private Menu orderStMenu;
	private Menu paymentStMenu;
	private Menu paymentTpMenu;
	private Menu logisticStMenu;
	private Menu returnStMenu;
	private Menu changeStatus;
	private Menu returnStatus;
	private Menu refundStatus;
	private Menu prodType;
	private Menu changeRule;

	public Menu getOrderStMenu() {
		return orderStMenu;
	}

	public void setOrderStMenu(Menu orderStMenu) {
		this.orderStMenu = orderStMenu;
	}

	public Menu getPaymentStMenu() {
		return paymentStMenu;
	}

	public void setPaymentStMenu(Menu paymentStMenu) {
		this.paymentStMenu = paymentStMenu;
	}

	public Menu getPaymentTpMenu() {
		return paymentTpMenu;
	}

	public void setPaymentTpMenu(Menu paymentTpMenu) {
		this.paymentTpMenu = paymentTpMenu;
	}

	public Menu getLogisticStMenu() {
		return logisticStMenu;
	}

	public void setLogisticStMenu(Menu logisticStMenu) {
		this.logisticStMenu = logisticStMenu;
	}

	public Menu getReturnStMenu() {
		return returnStMenu;
	}

	public void setReturnStMenu(Menu returnStMenu) {
		this.returnStMenu = returnStMenu;
	}

	public Menu getChangeStatus() {
		return changeStatus;
	}

	public void setChangeStatus(Menu changeStatus) {
		this.changeStatus = changeStatus;
	}

	public Menu getReturnStatus() {
		return returnStatus;
	}

	public void setReturnStatus(Menu returnStatus) {
		this.returnStatus = returnStatus;
	}

	public Menu getRefundStatus() {
		return refundStatus;
	}

	public void setRefundStatus(Menu refundStatus) {
		this.refundStatus = refundStatus;
	}

	public Menu getProdType() {
		return prodType;
	}

	public void setProdType(Menu prodType) {
		this.prodType = prodType;
	}

	/**
	 * @return the changeRule
	 */
	public Menu getChangeRule() {
		return changeRule;
	}

	/**
	 * @param changeRule the changeRule to set
	 */
	public void setChangeRule(Menu changeRule) {
		this.changeRule = changeRule;
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 6959335850386227441L;

	public String showOrders() throws Exception {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
				ServletActionContext.HTTP_RESPONSE);
		UserData userData = new UserData(request, response);
		if (null != userData && null != userData.getMemberId()) {

			String listType = request.getParameter("t");
			int page = Integer.parseInt(request.getParameter("p")) - 1;
			if ("T".equals(listType)) {
				Map<String, Object> htmlContent = gos.getActiveOrders(userData.getMemberId(), page);
				request.setAttribute("htmlContent", htmlContent);
				request.setAttribute("type", "T");
			} else if ("Y".equals(listType)) {
				Map<String, Object> htmlContent = gos.getYetPaymentOrders(userData.getMemberId(), page);
				request.setAttribute("htmlContent", htmlContent);
				request.setAttribute("type", "Y");
			} else if ("S".equals(listType)) {// 拆单
				Map<String, Object> htmlContent = gos.getSplitOrders(userData.getMemberId(), page);
				request.setAttribute("htmlContent", htmlContent);
				request.setAttribute("type", "S");
			} else if ("H".equals(listType)) {
				Map<String, Object> htmlContent = gos.getHistoryOrders(userData.getMemberId(), page);
				request.setAttribute("htmlContent", htmlContent);
				request.setAttribute("type", "H");
			} else if ("HY".equals(listType)) {
				Map<String, Object> htmlContent = gos.getHistoryYetPaymentOrders(userData.getMemberId(), page);
				request.setAttribute("htmlContent", htmlContent);
				request.setAttribute("type", "HY");
			} else if ("HS".equals(listType)) {
				Map<String, Object> htmlContent = gos.getHistorySplitOrders(userData.getMemberId(), page);
				request.setAttribute("htmlContent", htmlContent);
				request.setAttribute("type", "HS");
			} else if ("HSP".equals(listType)) {// 查詢
				if (StringUtils.isNotBlank(request.getParameter("s"))) {
					String selectParam = new String(request.getParameter("s").getBytes("iso8859-1"), "utf-8");
					if (StringUtils.isNotBlank(selectParam)) {
						Map<String, Object> htmlContent = gos.getHistoryOrdersSelect(userData.getMemberId(), page, selectParam);
						request.setAttribute("htmlContent", htmlContent);
						request.setAttribute("type", "HSP");
					}
				}
			} else if (listType.startsWith("G")) {// GY,GT,GH
				Map<String, Object> htmlContent = gos.getTuangouOrders(userData.getMemberId(), page, listType);
				request.setAttribute("htmlContent", htmlContent);
				request.setAttribute("type", listType);
			} else if ("OP".equals(listType)) {
				// 集購抽獎
				Map<String, Object> htmlContent = gos.getTuangouOrderPrizes(userData.getMemberId(), page);
				request.setAttribute("htmlContent", htmlContent);
				request.setAttribute("type", listType);
			}

			return "showOrders";
		} else {
			return "index";
		}
	}

	public void prepare() throws Exception {
		clearErrorsAndMessages();
		if (orderStMenu == null) {
			orderStMenu = menuService.get("Order.status");
		}
		if (logisticStMenu == null) {
			logisticStMenu = menuService.get("Order.Prod.LogisticSt");
		}
		if (paymentStMenu == null) {
			paymentStMenu = menuService.get("Order.paymentSt");
		}
		if (paymentTpMenu == null) {
			paymentTpMenu = menuService.get("Order.PaymentTp");
		}
		if (returnStMenu == null) {
			returnStMenu = menuService.get("Order.Prod.ReturnSt");
		}
		if (changeStatus == null) {
			changeStatus = menuService.get("ServiceOrder.change.status");
		}
		if (returnStatus == null) {
			returnStatus = menuService.get("ServiceOrder.return.status");
		}
		if (refundStatus == null) {
			refundStatus = menuService.get("ServiceOrder.fmReturnSt");
		}
		if (prodType == null) {
			prodType = menuService.get("Product.Type");
		}
		if (changeRule == null) {
			changeRule = menuService.get("Order.ReturnRule");
		}

	}
}
